<template>
  <div class="app-container">
    <el-row>
      <el-col :span="12">
        <div class="grid-content bg-purple">
          <el-button type="success" @click="addDataBtn">新增</el-button>
          <el-button type="warning" @click="moreDel">高级搜索</el-button>
        </div>
      </el-col>
      <el-col :span="12">
        <div class="grid-content bg-purple-light">
          <el-input
            v-model="iptSearch"
            placeholder="请输入内容"
            class="input-with-select"
          >
            <!-- <el-button slot="append" icon="el-icon-search" @click="onSearch"></el-button> -->
          </el-input>
        </div>
      </el-col>
    </el-row>

    <el-table
      style="width: 100%; margin: 10px auto"
      @selection-change="handleSelectionChange"
      @sort-change="onSortChange"
      height="450"
      :data="tableData"
    >
      <el-table-column
        fixed
        type="index"
        label="#"
        width="50"
      ></el-table-column>
      <el-table-column
        fixed
        prop="title"
        label="题目"
        width="300"
      ></el-table-column>
      <el-table-column prop="type" label="分类" width="200"></el-table-column>
      <el-table-column prop="author" label="作者" width="200">
        <template>尚甜</template>
      </el-table-column>
      <el-table-column
        prop="date"
        label="日期"
        sortable
        width="200"
      ></el-table-column>
      <el-table-column fixed="right" label="操作" width="300">
        <template slot-scope="scope">
          <el-button type="info" size="small">详情</el-button>
          <el-button
            type="danger"
            size="small"
            @click="delDataClk(scope.row.id)"
            >删除</el-button
          >
          <el-button type="warning" size="small" @click="upDataClk(scope.row)"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 抽屉 -->
    <el-drawer
      :title="title"
      :visible.sync="drawer"
      :before-close="handleClose"
      :with-header="true"
      size="50%"
    >
      <el-form
        :model="form"
        size="mini"
        style="padding: 0 5%"
        ref="form"
        label-width="100px"
        class="demo-form"
      >
        <el-form-item label="标题" prop="desc">
          <el-input
            type="textarea"
            v-model="form.title"
            rows="1"
          ></el-input>
        </el-form-item>
        <el-form-item label="类别" prop="resource">
          <el-select v-model="form.cid" placeholder="请选择类别">
            <el-option
              v-for="(v, i) in tableData"
              :key="i"
              :label="v.name"
              :value="v.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选项" prop="type">
          <div class="options">
            <div v-for="(item, index) in arr" :key="index">
              <div
                style="
                  display: flex;
                  justify-content: space-around;
                  align-items: baseline;
                "
              >
                <el-checkbox name="type" v-model="item.checked">{{
                  item.index
                }}</el-checkbox>
                <el-input
                 class="textareas"
                  type="textarea"
                  rows="1"
                  clos="15"
                  v-model="item.value"
                ></el-input>
              </div>
            </div>
            <div class="right">
              <el-button type="success" circle>加</el-button>
              <el-button type="danger" circle>减</el-button>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="答案提示" prop="desc">
          <el-input type="textarea" rows="1"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="addClk(form)">{{
            btnText
          }}</el-button>
          <el-button @click="cancelClk">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </div>
</template>

<script>
// import { mavonEditor } from "mavon-editor";
// import "mavon-editor/dist/css/index.css";
// import marked from "marked";
import { mapState, mapActions, mapMutations } from "vuex";
export default {
  // components:{
  //   mavonEditor,
  // },
  data() {
    return {
      drawer: false,
      form: {},
      title: "新增",
      btnText: "立即创建",
      arr: [
        { index: "A", value: "if(i<10;i++)", checked: false },
        { index: "B", value: "for(i=0;i<10)", checked: false },
        { index: "C", value: "for i=1 to 10 ", checked: false },
        { index: "D", value: "for(i=0;i<=10;i++)", checked: false },
      ],
      iptSearch: "",
      classify: [],
      currentPage: 1,
      pageSize: 5,
      total: 15,
      ruleForm: {
        name: "",
        region: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: "",
      },
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    ...mapActions("manage", ["getList"]),
    // 删除全部
    deleteAll(id) {
      // console.log(id);
      this.onDelete(this.arr);
    },
    //确认添加提交时
    addClk() {
      this.drawer = false;
      if (this.form.id) {
        this.updateList(this.form);
      } else {
        this.addlist(this.form);
      }
      this.getList();

      this.form = {
        author: "situ",
      };
    },
    cancelClk() {
      this.drawer = false;
      //取消
    },
    //删除
    onDelete(t) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.delList(t);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //修改
    upDataClk(v) {
      this.drawer = true;
      this.title = "修改";
      this.update = "修改";
      this.form = { ...v };
    },
    //增加
    addDataBtn() {
      this.drawer = true;
      this.title = "新增";
    },
    //抽屉关闭
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    //多想删除
    handleSelectionChange(id) {
      // console.log(id);
      for (var i = 0; i < id.length; i++) {
        this.arr.push(id[i].id);
      }
      // console.log(arr)
    },
    //排序
    onSortChange(value) {
      console.log(val);
    },
    //一页多少条
    handleSizeChange(val) {
      this.SET_PAGESZ(val);
      this.getList();
    },
    //当前页
    handleCurrentChange(val) {
      // console.log(val);
      this.SET_CURRENT(val);
      this.getList();
    },
    // 搜索
    onChangeIpt(v) {
      this.SET_SEARCH(v);
      this.getList();
    },
    moreDel() {},
  },
  computed: {
    ...mapState("manage", ["tableData"]),
  },
};
</script>

<style scoped>
.options {
  display: flex;
  flex-direction: column;
}

.textareas {
  margin-left: 8px;
  margin-top: 10px;
}
</style>